
package dyyx.util;

import org.apache.commons.lang3.StringUtils;

public abstract class RunStatusUtil {
	
	
	public static final int MAX_ERROR_NUM = 300;
		
	public static final RunStatus runStatus = new RunStatus();
	public static final String DEFAULT_TAG = "0";

	/*
	private static final HostInfo hostInfo = HostInfoUtil.getHostInfo();
	
	static{
		String hostInfoStr = "null";
		if(hostInfo!=null){
			hostInfoStr = hostInfo.toString();
		}
		updateStaticInfo("hostInfo", hostInfoStr);
	}
	*/
	
	public static void logTime(String key,long time){
		if(key==null || time<0){
			return;
		}
		TimeInfo ti = runStatus.timeInfoMap.get(key);
		if(ti==null){
			ti = new TimeInfo();
			runStatus.timeInfoMap.put(key,ti);
		}
		if(time==0){
			ti.zeroCount.incrementAndGet();
			//return;
		}
		
		ti.sum.addAndGet(time);
		ti.count.incrementAndGet();
		
		if(time>ti.max){
			ti.max = time;
			ti.maxTime = System.currentTimeMillis();
		}
		ti.current = time;
		
		
		if(time<100){
			return;
		}	
		
		if(time>=100){
			ti.countGt100.getAndIncrement();
		}
		
		if(time>=200){
			ti.countGt200.getAndIncrement();
		}
		if(time>=500){
			ti.countGt500.getAndIncrement();
		}
		
		if(time>=1000){
			ti.countGt1000.getAndIncrement();
		}
	}
	
	public static void logInfo(String key,String info){
		if(StringUtils.isBlank(key) || StringUtils.isBlank(info)){
			return;
		}
		runStatus.infoMap.put(key, info);
	}
	
	public static void logError(Throwable e){
		if(e==null){
			return;
		}
		logError(DEFAULT_TAG,e);
	}

	

	public static void logError(String tag,Throwable e){
		if(e==null){
			return;
		}
		if(tag==null){
			tag = DEFAULT_TAG;
		}
		String name = e.getClass().getSimpleName();
		if(StringUtils.isBlank(name)){
			return;
		}
		runStatus.updateCount(tag+"#"+name);
		//runStatus.updateInfo(name, e+","+new Date());
		runStatus.logError(tag, e);

	}
	
	public static void updateStaticInfo(String key,String msg){
		runStatus.updateStaticInfo(key, msg);
	}


	
	
	/*
	public static String getHostIp(){
		if(hostInfo==null){
			return null;
		}
		return hostInfo.getIp();
	}
	
	public static String getHostName(){
		if(hostInfo==null){
			return null;
		}
		return hostInfo.getName();
	}
	*/
	
	
	
}
